
package Presentation;


import java.net.URL;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import Modele.IHistorique;

public class FenetreAfficherHistorique extends Scene
{
    
    private Group root1 = null;
    FenetrePrincipale    fp;
    
    public FenetreAfficherHistorique(Parent root, FenetrePrincipale fp) {
    
        super(root);
        this.fp = fp;
        this.root1 = (Group) root;
        this.build();
        // TODO Auto-generated constructor stub
    }
    
    TableView<IHistorique>  table = new TableView<IHistorique>();
    private IntegerProperty index = new SimpleIntegerProperty();
    private final HBox      hb    = new HBox();
    
    public final double getIndex() {
    
        return index.get();
    }
    public final void setIndex(Integer value) {
    
        index.set(value);
    }
    public IntegerProperty indexProperty() {
    
        return index;
    }
    
    void build() {
        System.out.println("rentre");
        final ObservableList<IHistorique> data = FXCollections.observableArrayList(fp.gestionNavigation.getListeHistorique());
        Scene scene = new Scene(new Group());
        
        final Label label = new Label("Historique");
        label.setFont(new Font("Arial", 20));
        
        table.setEditable(true);
        
        TableColumn dateCol = new TableColumn("Date");
        dateCol.setMinWidth(100);
        dateCol.setCellValueFactory(new PropertyValueFactory<IHistorique, String>("dateFormater"));
        
        TableColumn urlCol = new TableColumn("URL");
        urlCol.setMinWidth(300);
        urlCol.setCellValueFactory(new PropertyValueFactory<IHistorique, URL>("url"));

        
        table.setItems(data);
        table.getColumns().addAll(dateCol, urlCol);
        
        final Button suppButton = new Button("Supprimer");
        suppButton.setOnAction(new EventHandler<ActionEvent>()
        {
            
            @Override
            public void handle(ActionEvent e) {
            
                data.remove(index.get());
                if(index.getValue()==0){
                    fp.gestionNavigation.supprimerHistorique(index.get());
                }else fp.gestionNavigation.supprimerHistorique(index.getValue()+1);
                table.getSelectionModel().clearSelection();
                
            }
        });
        
        indexProperty().addListener(new ChangeListener()
        {
            
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal) {
            
            }
        });
        
        table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener()
        {
            
            @Override
            public void changed(ObservableValue observable, Object oldvalue, Object newValue) {
            
                IHistorique selectedURL = (IHistorique) newValue;
                setIndex(data.indexOf(newValue));
            }
        });
        
        final Button suppAllButton = new Button("supprimer tout");
        suppAllButton.setOnAction(new EventHandler<ActionEvent>()
        {
            
            @Override
            public void handle(ActionEvent e) {
            
                data.removeAll(data);
                table.getSelectionModel().clearSelection();
                fp.gestionNavigation.supprimerAllHistorique();
            }
        });
        
        final Button goButton = new Button("charger l'url");
        goButton.setOnAction(new EventHandler<ActionEvent>()
        {
            
            @Override
            public void handle(ActionEvent e) {
            
                URL url = data.get(index.get()).getUrl();
                fp.nouvellePage(url);
            }
        });
        
        hb.getChildren().addAll(suppButton, suppAllButton, goButton);
        hb.setSpacing(3);
        
        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table, hb);
        
        root1.getChildren().addAll(vbox);

    }
    
}
